﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Demo.Const;
using Demo.DtoParameters;
using Demo.Entities;
using Demo.Repository;

namespace Demo.Services
{
    public class EmployeeService : IEmployeeService
    {
        private readonly IEmployeeRepository _employeeRepository;

        public EmployeeService(IEmployeeRepository _employeeRepository)
        {
            this._employeeRepository =
                _employeeRepository ?? throw new ArgumentNullException(nameof(_employeeRepository));
        }

        public async Task<IEnumerable<Employee>> GetEmployeesAsync(Guid companyId, EmployeeDtoParameters parameters)
        {
            parameters ??= new EmployeeDtoParameters();
            return await _employeeRepository.GetEmployeesAsync(companyId, parameters);
        }

        public Task<Employee> GetEmployeeAsync(Guid companyId, Guid employeeId)
        {
            return _employeeRepository.GetEmployeeAsync(companyId, employeeId);
        }

        public void AddEmployee(Guid companyId, Employee employee)
        {
            if (employee.Id == DefaultValue.DefaultGuid)
            {
                employee.Id = new Guid();
            }

            _employeeRepository.AddEmployee(companyId, employee);

            SaveAsync();
        }

        public void UpdateEmployee(Employee employee)
        {
            _employeeRepository.UpdateEmployee(employee);
        }

        public void DeleteEmployee(Employee employee)
        {
            _employeeRepository.DeleteEmployee(employee);
            SaveAsync();
        }

        public void DeleteEmployees(params Employee[] employees)
        {
            _employeeRepository.DeleteEmployees(employees);
        }

        public void SaveAsync()
        {
            _employeeRepository.SaveAsync();
        }
    }
}